package clientView;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import object.District;
import utility.StringUtil;
import clientMain.Main;
import clientUtilGUI.UtilityGUI;
import clientView.Province_View.MouseListDis;
import database.DistrictDelegate;

public class District_Insert extends JDialog implements ActionListener{

	private JTextField txtDisTName;
	private JLabel lblDisTName;
	private JButton btnSave;
	private JButton btnCancel;
	private int id;
	public District_Insert(JFrame main,int PrId){
		super(main, "Thêm Quận Huyện", true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	    int height = 150;
	    int width = 420;
	    id = PrId;
	    setSize(419, 109);
	    setLocation((UtilityGUI.screenSize.width-width)/2, (UtilityGUI.screenSize.height-height)/2);
		setResizable(false);
	    initComponet();
	}
		
		public void initComponet()
		{	
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.5, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0};
		getContentPane().setLayout(gridBagLayout);
		
		lblDisTName = new JLabel("Quận-Huyện: ");
		lblDisTName.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblDisName = new GridBagConstraints();
		gbc_lblDisName.anchor = GridBagConstraints.WEST;
		gbc_lblDisName.insets = new Insets(15, 15, 5, 5);
		gbc_lblDisName.fill = GridBagConstraints.VERTICAL;
		gbc_lblDisName.gridx = 0;
		gbc_lblDisName.gridy = 0;
		getContentPane().add(lblDisTName, gbc_lblDisName);
		
		txtDisTName = new JTextField();
		txtDisTName.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				DistrictDelegate d = new DistrictDelegate();
				if(!d.CheckInsertDisName(StringUtil.removeUnwantedSpace(txtDisTName.getText()),id)&&txtDisTName.getText().length()>3){
					btnSave.setEnabled(true);
				}else{
					btnSave.setEnabled(false);
					
				}
			
			}
		});
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridwidth = 2;
		gbc_textField.insets = new Insets(15, 5, 5, 15);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 0;
		getContentPane().add(txtDisTName, gbc_textField);
		txtDisTName.setColumns(10);
		
		btnSave = new JButton("Lưu");
		btnSave.setEnabled(false);
		btnSave.setCursor(UtilityGUI.cursorBtn);
		btnSave.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.fill = GridBagConstraints.VERTICAL;
		gbc_btnSave.anchor = GridBagConstraints.EAST;
		gbc_btnSave.insets = new Insets(10, 0, 5, 5);
		gbc_btnSave.gridx = 1;
		gbc_btnSave.gridy = 2;
		getContentPane().add(btnSave, gbc_btnSave);
		btnSave.addActionListener(this);
		
		btnCancel = new JButton("Thoát");
		btnSave.setCursor(UtilityGUI.cursorBtn);
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.fill = GridBagConstraints.BOTH;
		gbc_btnCancel.insets = new Insets(10, 0, 5, 15);
		gbc_btnCancel.gridx = 2;
		gbc_btnCancel.gridy = 2;
		getContentPane().add(btnCancel, gbc_btnCancel);
		btnCancel.addActionListener(this);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnSave){
			if(!StringUtil.isValidName(txtDisTName.getText(),200,2)){
				JOptionPane.showMessageDialog(null, "Tên Quận-Huyện Ko để trống", 
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				return;
			}else{
				
				District dis = new District();
				dis.setProvinceId(id);
				dis.setDistrictName(txtDisTName.getText());
				DistrictDelegate disDelegate = new DistrictDelegate();
				boolean flag = disDelegate.insertDistrict(dis, Main.userId);
				if(flag == true){
					this.setVisible(false);
					JOptionPane.showMessageDialog(null, UtilityGUI.successAction, 
				    UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
					Province_View.tblDis.setVisible(false);
					Province_View.tblDis = UtilityGUI.createTblDistrict(id, 1, "DistrictId", true);
					Province_View. tblDis.addMouseListener(new MouseListDis()) ;	
					Province_View.scrollTblDis.setViewportView(Province_View.tblDis);
					dispose();
				}else{
					JOptionPane.showMessageDialog(null, UtilityGUI.errorAction, 
							UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
					return;
				}
			}
		}else if(e.getSource() == btnCancel){
			dispose();
		}
		
	}

}
